package com.s7.recycler.holder;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administor on 2018\7\11 0011.
 */
public abstract class BaseHolder extends RecyclerView.ViewHolder {

    private Map<Integer, View> mViewMap;

    public BaseHolder(View itemView) {
        super(itemView);
        mViewMap = new HashMap<>();
    }

    /**
     * 获取设置的view
     * @param id
     * @return
     */
    public <T extends View> T getView(int id) {
        View view = mViewMap.get(id);
        if (view == null) {
            view = itemView.findViewById(id);
            mViewMap.put(id, view);
        }
        return (T) view;
    }

    /**
     * 在Adapter中 对应位置
     */
    private int realPosition;

    /**
     * 设置对应位置
     * @param realPosition
     */
    public void setRealPosition(int realPosition) {
        this.realPosition = realPosition;
    }

    public int getRealPosition() {
        return realPosition;
    }

    /**
     * 设置控件宽高
     * @param id
     * @param width
     * @param height
     */
    public void setLayoutParams(int id, int width, int height) {
        getView(id).getLayoutParams().width = width;
        getView(id).getLayoutParams().height = height;
    }

    /**
     * 设置背景
     * @param id
     * @param color
     */
    public void setBackgroundColor(int id, @ColorInt int color) {
        getView(id).setBackgroundColor(color);
    }

    /**
     * 设置背景
     * @param id
     * @param resId
     */
    public void setBackgroundResource(int id, @DrawableRes int resId) {
        getView(id).setBackgroundResource(resId);
    }

    /**
     * 设置背景
     * @param id
     * @param drawable
     */
    public void setBackgroundDrawable(int id, Drawable drawable) {
        getView(id).setBackground(drawable);
    }

    /**
     * 设置tag
     * @param id
     * @param tag
     */
    public void setTag(int id, Object tag) {
        getView(id).setTag(tag);
    }

    /**
     * 设置图片
     * @param id
     * @param drawable
     */
    public void setImageDrawable(int id, @Nullable Drawable drawable) {
        ((ImageView) getView(id)).setImageDrawable(drawable);
    }

    /**
     * 设置图片
     * @param id
     * @param image
     */
    public void setImageResource(int id, @DrawableRes int image) {
        ((ImageView) getView(id)).setImageResource(image);
    }

    /**
     * TextView 设置文字
     * @param id
     * @param text
     */
    public void setText(int id, CharSequence text) {
        ((TextView)getView(id)).setText(text);
    }

    /**
     * TextView 设置图片
     * @param id
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setTextDrawable(int id, @Nullable Drawable left, @Nullable Drawable top,
                             @Nullable Drawable right, @Nullable Drawable bottom) {
        ((TextView)getView(id)).setCompoundDrawables(left, top, right, bottom);
    }

    /**
     * TextView 设置图片
     * @param id
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param padding
     */
    public void setTextDrawable(int id, @Nullable Drawable left, @Nullable Drawable top,
                             @Nullable Drawable right, @Nullable Drawable bottom, int padding) {
        ((TextView)getView(id)).setCompoundDrawables(left, top, right, bottom);
        ((TextView)getView(id)).setCompoundDrawablePadding(padding);
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setText(int id, @ColorInt int color) {
        ((TextView)getView(id)).setTextColor(color);
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setTextColor(int id, @ColorInt int color) {
        ((TextView)getView(id)).setTextColor(color);
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setTextColor(int id, String color) {
        ((TextView)getView(id)).setTextColor(Color.parseColor(color));
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setTextColorResources(int id, int color) {
        ((TextView)getView(id)).setTextColor(getView(id).getResources().getColor(color));
    }


    /**
     * Button 设置文字
     * @param id
     * @param text
     */
    public void setButton(int id, String text) {
        ((Button) getView(id)).setText(text);
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButton(int id, @ColorInt int color) {
        ((Button) getView(id)).setTextColor(color);
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButtonColor(int id, @ColorInt int color) {
        ((Button) getView(id)).setTextColor(color);
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButtonColor(int id, String color) {
        ((Button) getView(id)).setTextColor(Color.parseColor(color));
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButtonColorResources(int id, int color) {
        ((Button) getView(id)).setTextColor(getView(id).getResources().getColor(color));
    }

    /**
     * 设置控件是否显示
     * @param id
     * @param visibility
     */
    public void setVisibility(int id, int visibility) {
        getView(id).setVisibility(visibility);
    }

    /**
     * CheckBox 设置选中状态
     * @param id
     * @param checked
     */
    public void setChecked(int id, boolean checked) {
        ((CompoundButton) getView(id)).setChecked(checked);
    }

    /**
     * RatingBar 设置设置分级
     * @param id
     * @param rating
     */
    public void setRating(int id, float rating) {
        ((RatingBar) getView(id)).setRating(rating);
    }

}
